﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;

using Warehouse.Silverlight.Core.Infrastructure;
using Warehouse.Silverlight.Core.Infrastructure.ViewModels;
using Warehouse.Silverlight.Modules.Main.Models;
using Warehouse.Silverlight.Modules.Main.Services;

namespace Warehouse.Silverlight.Modules.Main.ViewModels
{
    [Export]
    public class NavTreeViewModel : ViewModel
    {
        private const string _moduleHeader = "MainModule";
        private readonly INavTreeService _navTreeService;
        private readonly IRegionManager _regionManager;
        private ObservableCollection<NavTreeItem> _items;
        private NavTreeItem _selectedItem;

        [ImportingConstructor]
        public NavTreeViewModel(INavTreeService navTreeService, IRegionManager regionManager)
		{
            _navTreeService = navTreeService;
            _regionManager = regionManager;

            Initialize();
		}

        public string ModuleHeader { get { return _moduleHeader; } }

        public ObservableCollection<NavTreeItem> Items { get { return _items; } }

        public NavTreeItem SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    RaisePropertyChanged(() => SelectedItem);

                    if (_selectedItem != null && !string.IsNullOrEmpty(_selectedItem.Link))
                    {
                        ContentRegion_RequestNavigate(_selectedItem.Link);
                    }
                }
            }
        }

        public ICommand SelectionChangedCommand { get; private set; }

        // Override
        protected override void Initialize()
        {
            _items = _navTreeService.GetAllItems();

            //SetupCommands();
        }

        #region private Helpers

        private void ContentRegion_RequestNavigate(string source)
        {
            _regionManager.RequestNavigate(RegionNames.ContentRegion, source);
        }

        //private void SetupCommands()
        //{
        //    SelectionChangedCommand = new DelegateCommand<SelectionChangedEventArgs>(
        //        (e) => this.OnSelectionChangedCommand(e));
        //}

        //private void OnSelectionChangedCommand(SelectionChangedEventArgs e)
        //{
        //    if (e.AddedItems.Count == 1)
        //    {
        //        var enumerator = e.AddedItems.GetEnumerator();
        //        enumerator.MoveNext();
        //        var c = (NavTreeItem)enumerator.Current;
        //    }
        //}

        #endregion
    }
}
